package com.zwb.demo.easyweb.modules.database.entity;

import com.zwb.demo.easyweb.modules.database.vo.DatabaseLinkVO;
import org.springframework.lang.Nullable;

import java.text.MessageFormat;

public enum DatabaseType {
    MYSQL(1) {
        @Override
        public String driverName() {
            return "com.mysql.jdbc.Driver";
        }

        @Override
        public String url(DatabaseLinkVO vo) {
            return MessageFormat.format(
                    "jdbc:mysql://{0}:{1}/{2}?useUnicode=true&characterEncoding=utf-8",
                    vo.getHost(), vo.getPort(), vo.getDatabaseName());
        }
    },
    ORACLE(2) {
        @Override
        public String driverName() {
            return "oracle.jdbc.driver.OracleDriver";
        }

        @Override
        public String url(DatabaseLinkVO vo) {
            return MessageFormat.format(
                    "jdbc:oracle:thin:@{0}:{1}:{2}",
                    vo.getHost(), vo.getPort(), vo.getDatabaseName());
        }
    };
    private final int value;

    DatabaseType(int value) {
        this.value = value;
    }

    public abstract String driverName();

    public abstract String url(DatabaseLinkVO vo);

    @Nullable
    public static DatabaseType typeOf(String value) {
        for (DatabaseType type : values()) {
            if (type.value == Integer.parseInt(value)) {
                return type;
            }
        }
        return null;
    }

    public int getValue() {
        return value;
    }
}
